﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MyCompanyName.AbpZeroTemplate.Daybooks
{
    using Abp.Domain.Repositories;
    using Abp.Domain.Services;

    /// <summary>
    /// 领域服务，业务类       继承 DomainService
    /// 若是业务简单，可以不要这个业务类，在Application那边，直接操作Repository
    /// </summary>
    public class DaybookManager : AbpZeroTemplateDomainServiceBase //内部设置好了LocalizationSourceName
    {
        public Abp.Runtime.Session.IAbpSession AbpSession { get; set; }
        protected IRepository<Daybook, long> _daybookRepository { get; private set; }
        protected IRepository<DaybookItem, long> _daybookItemRepository { get; private set; }

        public DaybookManager(IRepository<Daybook, long> daybookRepository, IRepository<DaybookItem, long> daybookItemRepository)
        {
            _daybookRepository = daybookRepository;
            _daybookItemRepository = daybookItemRepository;
            //LocalizationSourceName = "daybook";
            AbpSession = Abp.Runtime.Session.NullAbpSession.Instance;
        }

        public IQueryable<Daybook> Daybooks { get { return _daybookRepository.GetAll(); } }
        public IQueryable<DaybookItem> DaybookItems { get { return _daybookItemRepository.GetAll(); } }

        //[Abp.Domain.Uow.UnitOfWork]
        public virtual async Task CreateAsync(Daybook daybook)
        {
            ValidateDuplicate(daybook);

            if (AbpSession.TenantId.HasValue)
            {
                daybook.TenantId = AbpSession.TenantId;
                foreach (var item in daybook.Items)
                {
                    item.TenantId = AbpSession.TenantId;
                }
            }

            await _daybookRepository.InsertAsync(daybook);
        }

        public virtual async Task UpdateAsync(Daybook daybook)
        {
            ValidateDuplicate(daybook);
            if (AbpSession.TenantId.HasValue)
            {
                daybook.TenantId = AbpSession.TenantId;
                foreach (var item in daybook.Items)
                {
                    item.TenantId = AbpSession.TenantId;
                }
            }
            await _daybookRepository.UpdateAsync(daybook);
        }


        public virtual async Task UpdateAsync(DaybookItem item)
        {
            await _daybookItemRepository.UpdateAsync(item);
        }
        public virtual async Task DeleteAsync(DaybookItem item)
        {
            await _daybookItemRepository.DeleteAsync(item);
        }


        protected virtual void ValidateDuplicate(Daybook info)
        {
            var siblings = _daybookRepository.GetAll().Where(x => x.BookDay == info.BookDay && x.AccountId == info.AccountId);
            if (siblings.Any(x => x.Id != info.Id))
            {
                throw new Abp.UI.UserFriendlyException(L("DaybookDuplicateDayWarning", info.BookDay));
            }
        }

        #region query

        public Task<Daybook> GetByIdAsync(long id)
        {
            return _daybookRepository.GetAsync(id);
        }

        public Daybook GetById(long id)
        {
            return _daybookRepository.Get(id);
        }

        public Task<Daybook> FindByAsync(DateTime bookDay, long accountId)
        {
            return _daybookRepository.FirstOrDefaultAsync(x => x.BookDay == bookDay && x.AccountId == accountId);
        }


        public Daybook FindBy(DateTime bookDay, long accountId)
        {
            return _daybookRepository.FirstOrDefault(x => x.BookDay == bookDay && x.AccountId == accountId);
        }

        public Task<List<DaybookItem>> GetItemsByIdAsync(long bookId)
        {
            return _daybookItemRepository.GetAllListAsync(x=>x.DaybookId==bookId);
        }


        public List<DaybookItem> GetItemsById(long bookId)
        {
            return _daybookItemRepository.GetAllList(x => x.DaybookId == bookId);
        }


        #endregion
    }
}
